package com.my.bookstore.service;

import com.my.bookstore.dao.BookDAO;
import com.my.bookstore.dao.impl.BookDAOImpl;
import com.my.bookstore.domain.Book;
import com.my.bookstore.domain.ShoppingCart;
import com.my.bookstore.web.CriteriaBook;
import com.my.bookstore.web.Page;

/**
 * Created by YJH on 2017/7/18 14:42.
 */
public class BookService {

    private BookDAO bookDAO = new BookDAOImpl();

    public Page<Book> getPage(CriteriaBook criteriaBook) {
        return bookDAO.getPage(criteriaBook);
    }

    public Book getBook(int id) {
        return bookDAO.getBook(id);
    }

    public boolean addToCart(int id, ShoppingCart cart) {
        Book book = bookDAO.getBook(id);
        if (book != null) {
            cart.addBook(book);
            return true;
        }
        return false;
    }

    /**
     * 从购物车中移除一件商品
     */
    public void removeItemFromShoppingCart(ShoppingCart cart, int id) {
        cart.removeItem(id);
    }

    /**
     * 清空购物车
     */
    public void clearShoppingCart(ShoppingCart cart) {
        cart.clear();
    }

    /**
     * 修改商品件数
     */
    public void updateItemQuantity(ShoppingCart cart, int id, int quantity) {
        cart.updateItemQuantity(id,quantity);
    }
}
